<template>
  <div>
    <template>
      <div class="permission-container">
        <div class="app-container">
          <!-- 表格 -->
          <el-card>
            <div style="text-align: right; margin-bottom: 20px">
              <el-button type="primary" size="small" @click="doAdd('')">添加权限</el-button>
            </div>
            <el-table border :data="list" row-key="id" :tree-props="{ children: 'children',hasChildren: 'hasChildren' }">
              <el-table-column label="名称" prop="name" />
              <el-table-column label="标识" prop="code" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="text" @click="doAdd(scope.row.id)">添加</el-button>
                  <el-button type="text" @click="hEdit(scope.row.id)">编辑</el-button>
                  <el-button type="text" @click="doDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
          <!-- 添加的Dialog -->
          <el-dialog :show-close="false" :close-on-click-modal="false" :visible.sync="showDialog" :title="isEdit?'编辑权限':'添加权限'">
            <!-- 表单内容 -->
            <el-form ref="formData" :model="formData" label-width="100px" :rules="rules">
              <el-form-item label="权限名称" prop="name">
                <el-input v-model="formData.name" />
              </el-form-item>
              <el-form-item label="权限标识" prop="code">
                <el-input v-model="formData.code" />
              </el-form-item>
              <el-form-item label="权限描述" prop="description">
                <el-input v-model="formData.description" />
              </el-form-item>
              <el-form-item label="权限启用">
                <el-switch
                  v-model="formData.enVisible"
                  active-text="启用"
                  active-value="1"
                  inactive-text="不启用"
                  inactive-value="0"
                />
              </el-form-item>
            </el-form>

            <template #footer>
              <div style="text-align: right;">
                <el-button @click="hCancel">取消</el-button>
                <el-button type="primary" @click="hSubmit">确定</el-button>
              </div>
            </template>
          </el-dialog>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import { transArray } from '@/utils'
import { getPermissionList, addPermission, delPermission, getPermissionDetail, updatePermission } from '@/api/permission'
export default {
  name: '',
  data() {
    return {
      isEdit: false,
      list: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: 1 // 类型
      },
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }],
        description: [{ required: true, message: '请输入权限描述', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    this.getPermList()
  },
  methods: {
    async getPermList() {
      const res = await getPermissionList()
      console.log(res, '获取展示数据')
      this.list = transArray(res.data)
    },
    doAdd(pid) {
      this.isEdit = false
      this.showDialog = true
      this.formData.pid = pid
    },
    hCancel() {
      this.showDialog = false
      this.resetForm()
    },
    async hSubmit() {
      this.$refs.formData.validate(async valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.hAdd()
        }
      })
    },
    async hEdit(id) {
      this.isEdit = true
      const res = await getPermissionDetail(id)
      console.log(res)
      this.formData = res.data
      this.showDialog = true
    },
    async doEdit() {
      const res = await updatePermission(this.formData)
      console.log(res, '编辑权限')
      this.showDialog = false
      this.$message('success')
      this.getPermissionList()
    },
    async hAdd() {
      const res = await addPermission(this.formData)
      console.log(res, '添加权限')
      this.showDialog = false
      this.getPermList()
      this.$message.success('添加成功')
      this.resetForm()
    },
    doDel(id) {
      this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delPermission(id)
        console.log(res, '删除权限')
        this.getPermList()
        this.$message.success('删除成功')
      })
    },
    resetForm() {
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
